package com.dona.imsidetector.db.dao;

import com.dona.imsidetector.db.XUtilsManager;
import com.dona.imsidetector.db.entity.ListItem;

import org.xutils.DbManager;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.ex.DbException;

import java.util.ArrayList;
import java.util.List;

public class ItemDao {

    private static DbManager dbManager = XUtilsManager.getInstance().getDbManager();

    public static String add(ListItem item){
        try {
            dbManager.save(item);
            return null;
        }catch (DbException e){
            e.printStackTrace();
            return e.getMessage();
        }
    }


    public static String addAll(List<ListItem> listItems){
        try {
            for(int i = 0; i < listItems.size(); i++){
                ListItem listItem = listItems.get(i);
                if(findAllByListIdAndImsi(listItem.getListId(),listItem.getImsiAddr()) != null){
                    dbManager.update(listItem);
                }else {
                    dbManager.save(listItem);
                }

            }

            return null;
        }catch (DbException e){
            e.printStackTrace();
            return e.getMessage();
        }
    }

    public static String clear(){
        try {
            dbManager.delete(ListItem.class);
        }catch (DbException e){
            e.printStackTrace();
            return e.getMessage();
        }
        return null;
    }

    public static String update(ListItem item){
        try {
            dbManager.saveOrUpdate(item);
        }catch (DbException e){
            e.printStackTrace();
            return e.getMessage();
        }
        return null;
    }

    public static List<ListItem> findAllByListId(String listid){
        try {

            List<ListItem> all = dbManager.selector(ListItem.class).where("listId", "=", listid).findAll();
            if(null != all){
                int index = 0;
                List<ListItem> listItems = new ArrayList<>();
                for(ListItem listItem : all){
                    if(listItem.getIsWhite() == 0){
                        listItems.add(0, listItem);
                        index++;
                    }else {
                        listItems.add(index, listItem);
                    }
                }
                return listItems;
            }else {
                return null;
            }
        }catch (DbException e){
            e.printStackTrace();
            return null;
        }
    }

    public static String delete(ListItem listItem){
        try {
            dbManager.delete(listItem);
        }catch (DbException e){
            e.printStackTrace();
            return e.getMessage();
        }
        return null;
    }


    public static ListItem findAllByListIdAndImsi(String listid,String imsi){
        try {
            WhereBuilder wb = WhereBuilder.b("listId", "=", listid);
            wb.and("imsiAddr","=",imsi);
            return dbManager.selector(ListItem.class).where(wb).findFirst();

        }catch (DbException e){
            e.printStackTrace();
            return null;
        }

    }

    public static int deteteAllByListId(String listid){
        try {
            WhereBuilder wb = WhereBuilder.b("listId", "=", listid);
            return dbManager.delete(ListItem.class, wb);

        }catch (DbException e){
            e.printStackTrace();
            return -1;
        }
    }

//460006021826471 460023148087839 460048816003693
    // 根据名单的黑白名单的uuid查表单项
    public static List<ListItem> findBOrWListByListId(String listid, int isWhite){
        try {
            WhereBuilder b = WhereBuilder.b();
            b.and("listId", "=", listid);
            b.and("isWhite","=", isWhite);
            List<ListItem> all = dbManager.selector(ListItem.class).where(b).findAll();
            if(null != all){
                return all;
            }else {
                return null;
            }
        }catch (DbException e){
            e.printStackTrace();
            return null;
        }
    }

    public static void findByImsi() {
    }
}
